package com.space.school.controller;

import com.space.school.Enum.OrderStatus;
import com.space.school.domain.Order;
import com.space.school.domain.OrderMaster;
import com.space.school.domain.ProductInfo;
import com.space.school.services.OrderService;
import com.space.school.services.impl.OrderServiceImpl;
import com.space.school.utils.KeyUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet(name = "orderMasterServlet",value = "/orderMasterServlet")
public class OrderMasterServlet extends HttpServlet{
    OrderService orderService=new OrderServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //        查询以上架商品信息
        req.setCharacterEncoding("utf-8");
        List<ProductInfo> productInfoList=orderService.productList();
        req.setAttribute("productInfoList",productInfoList);
        req.getRequestDispatcher("insertOrderDetail.jsp").forward(req,resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String order_id=req.getParameter("order_id");
        String operate=req.getParameter("operate");
        if(operate.equals("back")){
            req.setAttribute("order_id",order_id);
            List<ProductInfo> productInfoList=orderService.productList();
            req.setAttribute("productInfoList",productInfoList);
            req.getRequestDispatcher("insertOrderDetail.jsp").forward(req,resp);
        }else if(operate.equals("exit")){
    //            退出操作
            orderService.delete(order_id);
            req.getRequestDispatcher("list.jsp").forward(req,resp);
        }else {
//            保存/购买

            int order_status= OrderStatus.UNFINISHED.getCode();
            int pay_status=0;
//             购买
            if(operate.equals("")){
                order_status=OrderStatus.FINISHED.getCode();
                pay_status=1;
            }
            OrderMaster orderMaster=new OrderMaster();
//            订单号
            orderMaster.setOrderId(order_id);
//            买家信息
            orderMaster.setBuyer_name(req.getParameter("buyer_name"));
            orderMaster.setBuyer_phone(req.getParameter("buyer_phone"));
            orderMaster.setBuyer_address(req.getParameter("buyer_address"));
            orderMaster.setBuyer_openid(req.getParameter("buyer_openid"));
//            总金额
            orderMaster.setOrder_amount(Integer.parseInt(req.getParameter("order_amount")));
//          总数量
            orderMaster.setOrder_total(Integer.parseInt(req.getParameter("order_total")));
//            状态
            orderMaster.setOrder_status(order_status);
            orderMaster.setPay_status(pay_status);
//          保存与购买
            orderService.insert(orderMaster);
            req.getRequestDispatcher("list.jsp").forward(req,resp);
        }

    }

}
